<template>
  <view>
    <uni-forms ref="bindForm" :modelValue="bindData" :rules="bindRules" label-position="left">
      <uni-forms-item label="学号" name="stuId">
        <uni-easyinput v-model="bindData.stuId" placeholder="请输入学号" type="number"/>
      </uni-forms-item>
      <uni-forms-item label="真实姓名" name="stuRealName">
        <uni-easyinput v-model="bindData.stuRealName" placeholder="请输入真实姓名" type="text"/>
      </uni-forms-item>
    </uni-forms>
    <button @click="bind">绑定</button>
  </view>
</template>

<script>
import {bindStu} from "../../api/student";

export default {
  data() {
    return {
      bindData: {
        stuId: "",
        stuRealName: ""
      },
      bindRules: {
        stuId: {
          rules: [{
            required: true,
            errorMessage: '请输入学号',
          },]
        },
        stuRealName: {
          rules: [{
            required: true,
            errorMessage: '请输入真实姓名',
          },]
        }
      }
    }
  },
  methods: {
    bind() {
      this.$refs.bindForm.validate().then(_ => {
        bindStu(this.bindData).then(res => {
          if (res.code !== 200) {
            uni.showToast({
              title: res.msg,
              duration: 3000,
              icon: 'error'
            })
          }else{
            uni.showToast({
              title: "绑定成功",
            })
            uni.navigateBack()
          }
        })
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style>

</style>
